import logging
import pandas as pd
from flask import  request, jsonify ,Blueprint

from service.modelServer import modelPredictServer
from util.webUtils import result


model_bp = Blueprint('modeController', __name__)
logger = logging.getLogger(__name__)
@model_bp.route('/predicted', methods=['POST'])
def predicted():
    logger.info('Predicted mode')

    # post请求需要以form-data的形式上传文件 且表单key=raw
    if "raw" not in request.files:
        logger.info('the key of file part in the request must contains raw')
        return jsonify({'error': 'No file part in the request'}), 400

    # post请求需要以form-data的形式上传文件 且表单key=raw
    logger.info(f"the {request.files['raw'].filename}  file  upload was successful " )

    df = pd.read_csv(request.files['raw'])
    predicted = modelPredictServer(df)
    if predicted == 'Wakefulness':
        return result(predicted)
    elif predicted == 'light_sleep':
        return result(predicted)
    elif predicted == 'deep_sleep':
        return result(predicted)
    else:
        return result(predicted)





